﻿using System.Windows.Controls;
using System.Windows.Navigation;
using Fayshine.VA.ViewModels;
using System.Collections.Generic;
using Fayshine.VA.RIAService.Web;
using System.Windows.Controls.Theming;
using System.Windows;
using Fayshine.VA.Helpers;


namespace Fayshine.VA.Views.SysConfig
{
    public partial class PosManage : Page
    {
        private POSViewModel m_ViewModel = null;

        public PosManage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(PosManage_Loaded);
        }

        void PosManage_Loaded(object sender, RoutedEventArgs e)
        {
            ValidUserRole();
            m_ViewModel = new POSViewModel();
            this.DataContext = m_ViewModel;

            m_ViewModel.GetAllAreas();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void btnAddPOS_Click(object sender, RoutedEventArgs e)
        {
            POSView addPosWin = new POSView("添加 POS 数据",null);
            addPosWin.Closed += new System.EventHandler(addPosWin_Closed);
            addPosWin.Show();
        }

        void addPosWin_Closed(object sender, System.EventArgs e)
        {
            POSView addPosWin = sender as POSView;
            if (addPosWin.EditPos != null && addPosWin.DialogResult.HasValue && addPosWin.DialogResult.Value)
            {
                m_ViewModel.AddPos(addPosWin.EditPos);
            }
        }

        private void btnDeletePOS_Click(object sender, RoutedEventArgs e)
        {
            var selectedPOSs = this.dataGrid.GetSelectedItems<pos>();
            if (selectedPOSs.Count <= 0)
            {
                MessageBox.Show("请选择要删除的POS数据");
            }
            else
            {
                if (MessageBox.Show("确定要删除选择的POS数据吗?", "确定", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    if (m_ViewModel != null)
                    {
                        m_ViewModel.DeletePOSs(selectedPOSs);
                    }
                }
            }
        }

        private void btnUpdatePOS_Click(object sender, RoutedEventArgs e)
        {
            pos selectedPOS = this.dataGrid.SelectedItem as pos;
            if (selectedPOS == null)
            {
                MessageBox.Show("请选择要编辑的POS数据");
            }
            else
            {
                POSView editPosWin = new POSView("编辑 POS 数据", selectedPOS);
                editPosWin.Closed += new System.EventHandler(editPosWin_Closed);
                editPosWin.Show();
            }
        }

        void editPosWin_Closed(object sender, System.EventArgs e)
        {
            POSView userWindow = sender as POSView;
            if (userWindow.EditPos != null && userWindow.DialogResult.HasValue && userWindow.DialogResult.Value)
            {
                m_ViewModel.Update(userWindow.EditPos);
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (this.m_ViewModel != null)
            {
                this.m_ViewModel.SaveData2();
            }
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            if (datePager.SelectedDate.HasValue && cmbShops.SelectedValue != null)
            {
                int shopID = int.Parse(cmbShops.SelectedValue.ToString());
                if (this.m_ViewModel != null)
                {
                    this.m_ViewModel.GetPOSByDate(datePager.SelectedDate.Value, shopID);
                }
            }
        }

        private void ValidUserRole()
        {
            user user = SessionManager.Session["UserInfo"] as user;
            if (user != null)
            {
                if (user.RoleID == 3)
                {
                    this.btnAddPOS.IsEnabled = false;
                    this.btnDeletePOS.IsEnabled = false;
                    this.btnSave.IsEnabled = false;
                    this.dataGrid.IsReadOnly = true;
                }
                else
                {
                    this.btnAddPOS.IsEnabled = true;
                    this.btnDeletePOS.IsEnabled = true;
                    this.btnSave.IsEnabled = true;
                    this.dataGrid.IsReadOnly = false;
                }
            }
        }

    }
}
